package com.fushenlan.epidemicControl.aspect;

import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

/**
 * 接口入参及响应打印日志切面
 *
 * @author Administrator
 * @date 2022/4/21 9:39
 */
@Slf4j
@Aspect
@Component
public class WebLogAspect {

    @Pointcut("execution(public * com.fushenlan.epidemicControl.controller.*.*(..))")
    public void webLog() {
    }

    /**
     * 环绕日志打印
     *
     * @param joinPoint 切点
     * @return
     * @throws Throwable
     */
    @Around(value = "webLog()")
    public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
        String className = joinPoint.getTarget().getClass().getName();
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        StringBuffer buffer = new StringBuffer();
        buffer.append(className.replaceFirst("com.fushenlan", "【PC】")).append(".").append(signature.getMethod().getName()).append("().");
        log.info("====> {} param:{}", buffer, JSONUtil.toJsonStr(joinPoint.getArgs()));
        Object proceed = joinPoint.proceed();
        log.info("====> {} Return:{}", buffer, JSONUtil.toJsonStr(proceed));
        return proceed;
    }
}
